package com.jingdun.sport.api.controller;

import com.jingdun.sport.bean.app.param.BindMobile;
import com.jingdun.sport.bean.app.param.BindWeixin;
import com.jingdun.sport.bean.app.param.RegisterMobile;
import com.jingdun.sport.bean.app.param.RegisterWeixin;
import com.jingdun.sport.security.exception.WxErrorExceptionBase;
import com.jingdun.sport.security.service.YamiUser;
import com.jingdun.sport.security.util.SecurityUtils;
import com.jingdun.sport.service.UserAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
@AllArgsConstructor
@Slf4j
public class UserAuthController {
    @Autowired
    private UserAuthService userAuthService;

    @PostMapping("/bind/mobile")
    @ApiOperation(value = "绑定手机号", notes = "根据用户id绑定用户手机号")
    public ResponseEntity<String> bindPhone(@RequestBody BindMobile bindMobile) {
        YamiUser user = SecurityUtils.getUser();
        try {
            userAuthService.bindMobile(bindMobile, user.getUserId());
        }
        catch (WxErrorExceptionBase ex) {
            return ResponseEntity.badRequest().body(ex.getMessage());
        }
        return ResponseEntity.ok("ok");
    }

    @PostMapping("/register/mobile")
    public ResponseEntity<String> registerMobile(@RequestBody RegisterMobile registerMobile) {
        try {
            userAuthService.registerMobile(registerMobile);
        }
        catch (WxErrorExceptionBase ex) {
            return ResponseEntity.badRequest().body(ex.getMessage());
        }
        return ResponseEntity.ok("ok");
    }

    @PostMapping("/bind/weixin")
    @ApiOperation(value = "绑定手机号", notes = "根据用户id绑定用户手机号")
    public ResponseEntity<String> bindWeixin(@RequestBody BindWeixin bindWeixin) {
        YamiUser user = SecurityUtils.getUser();
        try {
            userAuthService.bindWeixin(bindWeixin, user.getUserId());
        }
        catch (WxErrorExceptionBase ex) {
            return ResponseEntity.badRequest().body(ex.getMessage());
        }
        return ResponseEntity.ok("ok");
    }

    @PostMapping("/register/weixin")
    public ResponseEntity<String> registerWeixin(@RequestBody RegisterWeixin registerWeixin) {
        try {
            userAuthService.registerWeixin(registerWeixin);
        }
        catch (WxErrorExceptionBase ex) {
            return ResponseEntity.badRequest().body(ex.getMessage());
        }
        return ResponseEntity.ok("ok");
    }
}
